package com.ruoyi.web.controller.project.paper;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.project.paper.PaperInfo;
import com.ruoyi.system.domain.project.paper.PaperInfoTeacher;
import com.ruoyi.system.domain.project.paper.PaperVo;
import com.ruoyi.system.service.project.IPaperInfoService;
import com.ruoyi.system.service.project.IPaperInfoTeacherService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 论文信息Controller
 * 
 * @author ruoyi
 * @date 2025-07-27
 */
@RestController
@RequestMapping("/project/paper")
public class PaperInfoController extends BaseController
{
    @Autowired
    private IPaperInfoService paperInfoService;
    @Autowired
    private IPaperInfoTeacherService paperInfoTeacherService;
    /**
     * 查询论文信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PaperInfo paperInfo)
    {
        startPage();
        List<PaperInfo> list = paperInfoService.selectPaperInfoList(paperInfo);
        return getDataTable(list);
    }

    /**
     * 导出论文信息列表
     */
    @Log(title = "论文信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PaperInfo paperInfo,
                       @RequestParam(required = false) String exportFields)
    {
        List<PaperInfo> list = paperInfoService.selectPaperInfoList(paperInfo);
        List<PaperVo> listVo = new java.util.ArrayList<>();
        for(PaperInfo info : list){
            if(info.getTeachers()!=null){
                for(PaperInfoTeacher teacher : info.getTeachers()){
                    PaperVo vo = new PaperVo();
                    // 基本属性映射
                    vo.setPaperTitle(info.getPaperTitle());
                    vo.setDoiNumber(info.getDoiNumber());
                    vo.setPublicationName(info.getPublicationName());
                    vo.setPublishTime(info.getPublishTime());
                    vo.setSignatureStatus(info.getSignatureStatus());
                    vo.setInclusionStatus(info.getInclusionStatus());
                    vo.setPaperLevel(info.getPaperLevel());
                    vo.setUserName(teacher.getUserName());
                    vo.setUserType(teacher.getUserType());
                    vo.setRank(teacher.getRank());
                    listVo.add(vo);
                }
            }
        }
        ExcelUtil<PaperVo> util = new ExcelUtil<PaperVo>(PaperVo.class);
        // 如果传递了导出字段，则进行处理
        if (StringUtils.isNotBlank(exportFields)) {
            // 将逗号分隔的字段转为数组，调用showColumn指定显示字段
            String[] fields = exportFields.split(",");
            util.showColumn(fields); // 关键：使用ExcelUtil已有的showColumn方法
        }
        util.exportExcel(response, listVo, "论文信息数据");
    }

    /**
     * 获取论文信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(paperInfoService.selectPaperInfoById(id));
    }

    /**
     * 新增论文信息
     */
    @Log(title = "论文信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PaperInfo paperInfo)
    {
        PaperInfoTeacher ra = new PaperInfoTeacher();
        if(paperInfo.getId()!=null){
            ra.setPaperId(paperInfo.getId());
        }else {
            ra.setPaperId(-1L);
        }
        List<PaperInfoTeacher> teachers = paperInfoTeacherService.selectPaperInfoTeacherList(ra);
        Long currentUserId = paperInfo.getAuthorId() ;
        if(teachers != null && !teachers.isEmpty()){
            for (PaperInfoTeacher teacher : teachers) {
                if(teacher.getUserId().equals(String.valueOf(currentUserId))){
                    return error("此论文您已经参与并有名次记录，请勿重复添加");
                }
            }
        }

        return toAjax(paperInfoService.insertPaperInfo(paperInfo));
    }

    /**
     * 修改论文信息
     */
    @Log(title = "论文信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PaperInfo paperInfo)
    {
        return toAjax(paperInfoService.updatePaperInfo(paperInfo));
    }

    /**
     * 删除论文信息
     */
    @Log(title = "论文信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(paperInfoService.deletePaperInfoByIds(ids));
    }

    /**
     * 根据关键字查询科研获奖信息
     */
    @GetMapping("/getPaperInfoByTitle")
    public AjaxResult getPaperInfoByTitle(  String data)
    {
        PaperInfo ra = paperInfoService.getPaperInfoByTitle(data);
        return success(ra);
    }
}
